﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Aliyeye.Car.Net.Utility;

namespace Aliyeye.Car.Net.Dal
{
    public class Register
    {
        public Register(){ }
        public int Add(Model.Register model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@RegisterId", SqlDbType.Int,4),
					new SqlParameter("@EndKm", SqlDbType.Int,4),
					new SqlParameter("@Total", SqlDbType.Decimal,9),
					new SqlParameter("@OliPrie", SqlDbType.Decimal,9),
					new SqlParameter("@DriPrie", SqlDbType.Decimal,9),
					new SqlParameter("@wyPrice", SqlDbType.Decimal,9),
					new SqlParameter("@UpkeepPrice", SqlDbType.Decimal,9),
					new SqlParameter("@makePrice", SqlDbType.Decimal,9),
					new SqlParameter("@SafePrice", SqlDbType.Decimal,9),
					new SqlParameter("@lostPrice", SqlDbType.Decimal,9),
					new SqlParameter("@clearPrice", SqlDbType.Decimal,9),
					new SqlParameter("@SendCarPrice", SqlDbType.Decimal,9),
					new SqlParameter("@MendPrice", SqlDbType.Decimal,9),
					new SqlParameter("@DiffPrice", SqlDbType.Decimal,9),
					new SqlParameter("@OtherPri", SqlDbType.Decimal,9),
					new SqlParameter("@pLPrice", SqlDbType.Decimal,9),
					new SqlParameter("@Tip", SqlDbType.Decimal,9),
					new SqlParameter("@PriceCode", SqlDbType.NVarChar,50),
					new SqlParameter("@outPrie", SqlDbType.Decimal,9),
					new SqlParameter("@Price", SqlDbType.Decimal,9),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@ChenkUser", SqlDbType.NVarChar,50),
					new SqlParameter("@UserPri", SqlDbType.NVarChar,50),
					new SqlParameter("@shPrice", SqlDbType.Decimal,9),
					new SqlParameter("@IsClear", SqlDbType.Bit,1),
					new SqlParameter("@Marks", SqlDbType.NVarChar,500),
					new SqlParameter("@Time", SqlDbType.DateTime),
					new SqlParameter("@State", SqlDbType.NVarChar,50),
					new SqlParameter("@InpuTime", SqlDbType.DateTime),
					new SqlParameter("@OutTime", SqlDbType.DateTime),
					new SqlParameter("@PrieMark", SqlDbType.NVarChar,500),
					new SqlParameter("@BPrice", SqlDbType.Decimal,9),
					new SqlParameter("@day", SqlDbType.Int,4),
					new SqlParameter("@CarSate", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.RegisterId;
            parameters[2].Value = model.EndKm;
            parameters[3].Value = model.Total;
            parameters[4].Value = model.OliPrie;
            parameters[5].Value = model.DriPrie;
            parameters[6].Value = model.wyPrice;
            parameters[7].Value = model.UpkeepPrice;
            parameters[8].Value = model.makePrice;
            parameters[9].Value = model.SafePrice;
            parameters[10].Value = model.lostPrice;
            parameters[11].Value = model.clearPrice;
            parameters[12].Value = model.SendCarPrice;
            parameters[13].Value = model.MendPrice;
            parameters[14].Value = model.DiffPrice;
            parameters[15].Value = model.OtherPri;
            parameters[16].Value = model.pLPrice;
            parameters[17].Value = model.Tip;
            parameters[18].Value = model.PriceCode;
            parameters[19].Value = model.outPrie;
            parameters[20].Value = model.Price;
            parameters[21].Value = model.UserId;
            parameters[22].Value = model.ChenkUser;
            parameters[23].Value = model.UserPri;
            parameters[24].Value = model.shPrice;
            parameters[25].Value = model.IsClear;
            parameters[26].Value = model.Marks;
            parameters[27].Value = model.Time;
            parameters[28].Value = model.State;
            parameters[29].Value = model.InpuTime;
            parameters[30].Value = model.OutTime;
            parameters[31].Value = model.PrieMark;
            parameters[32].Value = model.BPrice;
            parameters[33].Value = model.day;
            parameters[34].Value = model.CarSate;

            int roleId = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Register_ADD", parameters);
            if (roleId > 0)
            {
                return roleId;
            }
            else
            {
                return 0;
            }
        }
        public bool Update(Model.Register model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@RegisterId", SqlDbType.Int,4),
					new SqlParameter("@EndKm", SqlDbType.Int,4),
					new SqlParameter("@Total", SqlDbType.Decimal,9),
					new SqlParameter("@OliPrie", SqlDbType.Decimal,9),
					new SqlParameter("@DriPrie", SqlDbType.Decimal,9),
					new SqlParameter("@wyPrice", SqlDbType.Decimal,9),
					new SqlParameter("@UpkeepPrice", SqlDbType.Decimal,9),
					new SqlParameter("@makePrice", SqlDbType.Decimal,9),
					new SqlParameter("@SafePrice", SqlDbType.Decimal,9),
					new SqlParameter("@lostPrice", SqlDbType.Decimal,9),
					new SqlParameter("@clearPrice", SqlDbType.Decimal,9),
					new SqlParameter("@SendCarPrice", SqlDbType.Decimal,9),
					new SqlParameter("@MendPrice", SqlDbType.Decimal,9),
					new SqlParameter("@DiffPrice", SqlDbType.Decimal,9),
					new SqlParameter("@OtherPri", SqlDbType.Decimal,9),
					new SqlParameter("@pLPrice", SqlDbType.Decimal,9),
					new SqlParameter("@Tip", SqlDbType.Decimal,9),
					new SqlParameter("@PriceCode", SqlDbType.NVarChar,50),
					new SqlParameter("@outPrie", SqlDbType.Decimal,9),
					new SqlParameter("@Price", SqlDbType.Decimal,9),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@ChenkUser", SqlDbType.NVarChar,50),
					new SqlParameter("@UserPri", SqlDbType.NVarChar,50),
					new SqlParameter("@shPrice", SqlDbType.Decimal,9),
					new SqlParameter("@IsClear", SqlDbType.Bit,1),
					new SqlParameter("@Marks", SqlDbType.NVarChar,500),
					new SqlParameter("@Time", SqlDbType.DateTime),
					new SqlParameter("@State", SqlDbType.NVarChar,50),
					new SqlParameter("@InpuTime", SqlDbType.DateTime),
					new SqlParameter("@OutTime", SqlDbType.DateTime),
					new SqlParameter("@PrieMark", SqlDbType.NVarChar,500),
					new SqlParameter("@BPrice", SqlDbType.Decimal,9),
					new SqlParameter("@day", SqlDbType.Int,4),
					new SqlParameter("@CarSate", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.RegisterId;
            parameters[2].Value = model.EndKm;
            parameters[3].Value = model.Total;
            parameters[4].Value = model.OliPrie;
            parameters[5].Value = model.DriPrie;
            parameters[6].Value = model.wyPrice;
            parameters[7].Value = model.UpkeepPrice;
            parameters[8].Value = model.makePrice;
            parameters[9].Value = model.SafePrice;
            parameters[10].Value = model.lostPrice;
            parameters[11].Value = model.clearPrice;
            parameters[12].Value = model.SendCarPrice;
            parameters[13].Value = model.MendPrice;
            parameters[14].Value = model.DiffPrice;
            parameters[15].Value = model.OtherPri;
            parameters[16].Value = model.pLPrice;
            parameters[17].Value = model.Tip;
            parameters[18].Value = model.PriceCode;
            parameters[19].Value = model.outPrie;
            parameters[20].Value = model.Price;
            parameters[21].Value = model.UserId;
            parameters[22].Value = model.ChenkUser;
            parameters[23].Value = model.UserPri;
            parameters[24].Value = model.shPrice;
            parameters[25].Value = model.IsClear;
            parameters[26].Value = model.Marks;
            parameters[27].Value = model.Time;
            parameters[28].Value = model.State;
            parameters[29].Value = model.InpuTime;
            parameters[30].Value = model.OutTime;
            parameters[31].Value = model.PrieMark;
            parameters[32].Value = model.BPrice;
            parameters[33].Value = model.day;
            parameters[34].Value = model.CarSate;

            int intResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Register_Update", parameters);

            if (intResult > 0)
                return true;
            else
                return false;
        }
        public bool Delete(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;
            int intResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Register_Delete", parameters);

            if (intResult > 0)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Register GetModel(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            Model.Register model = new Model.Register();
            DataSet ds =SqlHelper.GetDataFormDataSet(CommandType.StoredProcedure,"Register_GetModel",parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RegisterId"].ToString() != "")
                {
                    model.RegisterId = int.Parse(ds.Tables[0].Rows[0]["RegisterId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EndKm"].ToString() != "")
                {
                    model.EndKm = int.Parse(ds.Tables[0].Rows[0]["EndKm"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Total"].ToString() != "")
                {
                    model.Total = decimal.Parse(ds.Tables[0].Rows[0]["Total"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OliPrie"].ToString() != "")
                {
                    model.OliPrie = decimal.Parse(ds.Tables[0].Rows[0]["OliPrie"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DriPrie"].ToString() != "")
                {
                    model.DriPrie = decimal.Parse(ds.Tables[0].Rows[0]["DriPrie"].ToString());
                }
                if (ds.Tables[0].Rows[0]["wyPrice"].ToString() != "")
                {
                    model.wyPrice = decimal.Parse(ds.Tables[0].Rows[0]["wyPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UpkeepPrice"].ToString() != "")
                {
                    model.UpkeepPrice = decimal.Parse(ds.Tables[0].Rows[0]["UpkeepPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["makePrice"].ToString() != "")
                {
                    model.makePrice = decimal.Parse(ds.Tables[0].Rows[0]["makePrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SafePrice"].ToString() != "")
                {
                    model.SafePrice = decimal.Parse(ds.Tables[0].Rows[0]["SafePrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["lostPrice"].ToString() != "")
                {
                    model.lostPrice = decimal.Parse(ds.Tables[0].Rows[0]["lostPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["clearPrice"].ToString() != "")
                {
                    model.clearPrice = decimal.Parse(ds.Tables[0].Rows[0]["clearPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SendCarPrice"].ToString() != "")
                {
                    model.SendCarPrice = decimal.Parse(ds.Tables[0].Rows[0]["SendCarPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MendPrice"].ToString() != "")
                {
                    model.MendPrice = decimal.Parse(ds.Tables[0].Rows[0]["MendPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DiffPrice"].ToString() != "")
                {
                    model.DiffPrice = decimal.Parse(ds.Tables[0].Rows[0]["DiffPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OtherPri"].ToString() != "")
                {
                    model.OtherPri = decimal.Parse(ds.Tables[0].Rows[0]["OtherPri"].ToString());
                }
                if (ds.Tables[0].Rows[0]["pLPrice"].ToString() != "")
                {
                    model.pLPrice = decimal.Parse(ds.Tables[0].Rows[0]["pLPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Tip"].ToString() != "")
                {
                    model.Tip = decimal.Parse(ds.Tables[0].Rows[0]["Tip"].ToString());
                }
                model.PriceCode = ds.Tables[0].Rows[0]["PriceCode"].ToString();
                if (ds.Tables[0].Rows[0]["outPrie"].ToString() != "")
                {
                    model.outPrie = decimal.Parse(ds.Tables[0].Rows[0]["outPrie"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Price"].ToString() != "")
                {
                    model.Price = decimal.Parse(ds.Tables[0].Rows[0]["Price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserId"].ToString() != "")
                {
                    model.UserId = int.Parse(ds.Tables[0].Rows[0]["UserId"].ToString());
                }
                model.ChenkUser = ds.Tables[0].Rows[0]["ChenkUser"].ToString();
                model.UserPri = ds.Tables[0].Rows[0]["UserPri"].ToString();
                if (ds.Tables[0].Rows[0]["shPrice"].ToString() != "")
                {
                    model.shPrice = decimal.Parse(ds.Tables[0].Rows[0]["shPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsClear"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsClear"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsClear"].ToString().ToLower() == "true"))
                    {
                        model.IsClear = true;
                    }
                    else
                    {
                        model.IsClear = false;
                    }
                }
                model.Marks = ds.Tables[0].Rows[0]["Marks"].ToString();
                if (ds.Tables[0].Rows[0]["Time"].ToString() != "")
                {
                    model.Time = DateTime.Parse(ds.Tables[0].Rows[0]["Time"].ToString());
                }
                model.State = ds.Tables[0].Rows[0]["State"].ToString();
                if (ds.Tables[0].Rows[0]["InpuTime"].ToString() != "")
                {
                    model.InpuTime = DateTime.Parse(ds.Tables[0].Rows[0]["InpuTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OutTime"].ToString() != "")
                {
                    model.OutTime = DateTime.Parse(ds.Tables[0].Rows[0]["OutTime"].ToString());
                }
                model.PrieMark = ds.Tables[0].Rows[0]["PrieMark"].ToString();
                if (ds.Tables[0].Rows[0]["BPrice"].ToString() != "")
                {
                    model.BPrice = decimal.Parse(ds.Tables[0].Rows[0]["BPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["day"].ToString() != "")
                {
                    model.day = int.Parse(ds.Tables[0].Rows[0]["day"].ToString());
                }
                model.CarSate = ds.Tables[0].Rows[0]["CarSate"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,RegisterId,EndKm,Total,OliPrie,DriPrie,wyPrice,UpkeepPrice,makePrice,SafePrice,lostPrice,clearPrice,SendCarPrice,MendPrice,DiffPrice,OtherPri,pLPrice,Tip,PriceCode,outPrie,Price,UserId,ChenkUser,UserPri,shPrice,IsClear,Marks,Time,State,InpuTime,OutTime,PrieMark,BPrice,day,CarSate ");
            strSql.Append(" FROM Register ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqlHelper.Query(strSql.ToString());
        }
    }
}
